package com.barray.log_monitor;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class LogAdapter extends RecyclerView.Adapter<LogAdapter.LogViewHolder> {
    private List<LogBean> logBeans = new ArrayList<>();
    @NonNull
    @Override
    public LogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_log,parent,false);
        return new LogViewHolder(view);
    }

    public void setNewDatas(List<LogBean> newDatas){
        logBeans.clear();
        logBeans.addAll(newDatas);
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull LogViewHolder holder, int position) {
        LogBean logBean = logBeans.get(position);
        holder.tvFileName.setText(logBean.fileName);
        holder.tvFileSize.setText(logBean.fileSize);
        holder.itemView.setOnClickListener(v -> {
            LogDetailActivity.start(v.getContext(),logBean);
        });
    }

    @Override
    public int getItemCount() {
        return logBeans.size();
    }

    public class LogViewHolder extends RecyclerView.ViewHolder{
        TextView tvFileName,tvFileSize;

        public LogViewHolder(@NonNull View itemView) {
            super(itemView);
            tvFileName = itemView.findViewById(R.id.tv_file_name);
            tvFileSize = itemView.findViewById(R.id.tv_file_size);
        }
    }
}
